package com.adaptiveAgingAdmin.utils;

import com.google.gson.Gson;

/**
 * Gson工具类,通用类与JSON的简单转换
 *
 * @author 张涛
 */
public class GsonUtil {
    private static Gson gson;
    static {
        gson=new Gson();
    }

    /**
     *
     * @param object
     * @return
     */
    public static String objectToJson(Object object){
        try {
            return gson.toJson(object);
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    /**
     *
     * @param json
     * @param tClass 需要转成的类
     * @return
     */
    public static <T extends Object> T parseJson(String json,Class<T> tClass){
        try {
            return gson.fromJson(json,tClass);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
